/*
 * 
 * Version 1.00
 * 
 * Created on 2007-6-4
 * Encoding GB2312
 */
package net.comm.user.service.impl;

import java.sql.Connection;
import java.util.List;

import net.comm.common.ReturnMessage;
import net.comm.common.common;


import net.comm.user.bean.BaseContacts;
import net.comm.user.bean.BaseUsers;
import net.comm.user.db.ContactsServiceSqlMysql;
import net.comm.user.db.DbContactsService;
import net.comm.user.db.DbUsersService;
import net.comm.user.db.UsersServiceSqlMysql;
import net.comm.user.web.form.ContactsForm;
import net.comm.util.DatetimeUtil;
import net.comm.util.DbcpPool;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;
import org.sakaiproject.db.impl.BasicSqlService;


/**
 *
 * @author hcgong(hcgong@sina.com)
 * @version 1.0
 * @date 2007-6-4
 */
public class ContactsServiceImpl {

	static Logger logger = Logger.getLogger(ContactsServiceImpl.class.getName());

	/**
	 * 
	 * @see net.IUserbasicService.module.other.IUserService
	 * 		#list(net.swift.module.other.ExaQueryBean, net.swift.common.ReturnMessage)
	 */
	public List list(ContactsForm theForm,  ReturnMessage msg) 
	{
		List list = null;
		
		return list;
	}

	/**
	 * 返回根据会员ID查询的会员操作日志
	 */
	public ContactsForm list(int  userID) 
	{
		Connection cn = null;
		ContactsForm theForm = new ContactsForm();
		
		try
		{
			cn = DbcpPool.getConnection();
			
			//取得FORM中传入的查询参数
			Object[] fields = new Object[1];
			
			fields[0] = userID;			

			ContactsServiceSqlMysql servSql = new ContactsServiceSqlMysql();						
			BasicSqlService  bSqlServ = new BasicSqlService();

			/*
			 * 获得查询SQL语句
			 */
			String sql = servSql.getByUserIDSql();
									
			List list = bSqlServ.dbRead(cn, sql, fields, new DbContactsService());
			if(list.size() > 0){
				BaseContacts base = (BaseContacts)list.get(0);
				BeanUtils.copyProperties(theForm, base);
				
			}				
		    DbcpPool.returnConnection(cn);
		    
		} catch(Exception ex) 
		{
			/**
			 * 记录相关日志
			 */
			logger.error(ex.getMessage());
			
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return theForm;
	}
	/**
	 * @see net.IUserbasicService.module.other.IUserService
	 * 		#get(net.swift.db.tab_paramnostro, net.swift.common.ReturnMessage)
	 */
	public void get(ContactsForm theForm, ReturnMessage msg) 
	{
		Connection cn = null;
		try
		{
			cn = DbcpPool.getConnection();
			

			//取得FORM中传入的查询参数
			Object[] fields1 = new Object[1];
			
			fields1[0] = theForm.getUserID();

			ContactsServiceSqlMysql servSql = new ContactsServiceSqlMysql();
			
			String sql = servSql.getbykeySql();
			
			BasicSqlService  bSqlServ = new BasicSqlService();
						
			List list = bSqlServ.dbRead(cn, sql, fields1, new DbContactsService());
			if(list.size() > 0){
				BaseContacts base = (BaseContacts)list.get(0);
				BeanUtils.copyProperties(theForm, base);
				
				DbcpPool.closeTran(cn);			
				DbcpPool.returnConnection(cn);
				return;				
			}

			//如果不存在则新增一条记录
			DbcpPool.beginTran(cn);
			
			//读取users表取得信息
			
			UsersServiceSqlMysql userservSql = new UsersServiceSqlMysql();
			
			sql = userservSql.getbykeySql();
			
			list = bSqlServ.dbRead(cn, sql, fields1, new DbUsersService());
			if(list.size() == 0){
				common.setAppFailMsg(msg , " 取记录失败，请检查！");
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return;				
			}
			BaseUsers base = (BaseUsers)list.get(0);				
			
			Object[] fields = new Object[14];
			
			fields[0] = theForm.getUserID();
			fields[1] = "";
			fields[2] = "";
			fields[3] = "";
			fields[4] = "";
			fields[5] = "";
			fields[6] = "";
			fields[7] = base.getEmail();
			fields[8] = "";
			fields[9] = "";
			fields[10] = "";
			fields[11] = "";
			fields[12] = "";
			fields[13] = DatetimeUtil.getDatetime();
			
			sql = servSql.getInsertSql();
			
			boolean  res = bSqlServ.dbWrite(cn, sql, fields);
			if(!res){
				common.setAppFailMsg(msg , " 插入记录失败，请检查！");
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return;
			}
			DbcpPool.commitTran(cn);
			DbcpPool.closeTran(cn);			
			DbcpPool.returnConnection(cn);			
			
		} catch(Exception ex) 
		{
			logger.error("System error , param get about invite" + ex);
			common.setAppFailMsg(msg , ex.getMessage());
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return ;
	}


	/**
	 * 添加会员操作日志到表中
	 * @see net.IUserbasicService.module.other.IUserService#add(net.swift.common.ReturnMessage)
	 */
	public void add(ContactsForm theForm, ReturnMessage msg) 
	{
		Connection cn = null;
		try
		{
			cn = DbcpPool.getConnection();
			
			DbcpPool.beginTran(cn);

			add(cn, theForm, msg);
			
			DbcpPool.commitTran(cn);
			DbcpPool.closeTran(cn);			
			DbcpPool.returnConnection(cn);
			
		} catch(Exception ex) 
		{
			logger.error("System error , param get about invite" + ex);
			common.setAppFailMsg(msg , ex.getMessage());
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return ;
	}
	/**
	 * 添加会员操作日志到表中
	 * @see net.IUserbasicService.module.other.IUserService#add(net.swift.common.ReturnMessage)
	 */
	public void add(Connection cn, ContactsForm theForm, ReturnMessage msg) 
	{
		try
		{

			ContactsServiceSqlMysql servSql = new ContactsServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();
			
			//取得FORM中传入的查询参数
			Object[] fields = new Object[14];
			
			fields[0] = theForm.getUserID();
			fields[1] = theForm.getMobile();
			fields[2] = theForm.getTel();
			fields[3] = theForm.getFamtel();
			fields[4] = theForm.getFax();
			fields[5] = theForm.getQQ();
			fields[6] = theForm.getMsn();
			fields[7] = theForm.getEmail();
			fields[8] = theForm.getAddr();
			fields[9] = theForm.getPostcode();
			fields[10] = theForm.getOffwebsite();
			fields[11] = theForm.getPerwebsite();
			fields[12] = theForm.getBlog();
			fields[13] = DatetimeUtil.getDatetime();
			
			String sql = servSql.getInsertSql();
						
			boolean  res = bSqlServ.dbWrite(cn, sql, fields);
			if(!res){
				common.setAppFailMsg(msg , " 插入记录失败，请检查！");
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return;
			}
			
		} catch(Exception ex) 
		{
			logger.error("System error , param get about invite" + ex);
			common.setAppFailMsg(msg , ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}				
		} 
		
		return ;
	}	
	/**
	 * 删除一条指定记录
	 * @see net.IUserbasicService.module.other.IUserService#update(net.swift.common.ReturnMessage)
	 */
	public void delete(ContactsForm theForm, ReturnMessage msg) 
	{
		
		Connection cn = null;
		try
		{
			cn = DbcpPool.getConnection();
			
			DbcpPool.beginTran(cn);

			//取得FORM中传入的查询参数
			Object[] fields = new Object[1];
			
			fields[0] = theForm.getUserID();
			
			ContactsServiceSqlMysql servSql = new ContactsServiceSqlMysql();
			
			String sql = servSql.getDeleteSql();
			
			BasicSqlService  bSqlServ = new BasicSqlService();
								
			boolean  res = bSqlServ.dbWrite(cn, sql, fields);
			
			if(res){
				common.setAppFailMsg(msg , " 删除记录失败，请检查！");
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return;
			}
			
			DbcpPool.returnConnection(cn);
			return;
			
		} catch(Exception ex) 
		{
			logger.error("System error , param get about invite" + ex);
			common.setAppFailMsg(msg , ex.getMessage());
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return ;
	}

	/**
	 * 更新一条指定记录
	 * @see net.IUserbasicService.module.other.IUserService#update(net.swift.common.ReturnMessage)
	 */
	public void update(ContactsForm theForm, ReturnMessage msg) 
	{
		Connection cn = null;
		try
		{
			cn = DbcpPool.getConnection();
			
			DbcpPool.beginTran(cn);

			ContactsServiceSqlMysql servSql = new ContactsServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();

			//取原记录的信息			
			Object[] fields2 = new Object[1];

			fields2[0] = theForm.getUserID();
			
			String sql = servSql.getbykeySql();
		
			List list = bSqlServ.dbRead(cn, sql, fields2, new DbContactsService());
			if(list.size() == 0){
				
				add(cn, theForm, msg);
				if(msg.getErrCode() != 0){
					return;
				}
				DbcpPool.commitTran(cn);			
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return;				
			}
	
			//取得FORM中传入的查询参数
			Object[] fields = new Object[14];
			
			fields[0] = theForm.getMobile();
			fields[1] = theForm.getTel();
			fields[2] = theForm.getFamtel();
			fields[3] = theForm.getFax();
			fields[4] = theForm.getQQ();
			fields[5] = theForm.getMsn();
			fields[6] = theForm.getEmail();
			fields[7] = theForm.getAddr();
			fields[8] = theForm.getPostcode();
			fields[9] = theForm.getOffwebsite();
			fields[10] = theForm.getPerwebsite();
			fields[11] = theForm.getBlog();
			fields[12] = DatetimeUtil.getDatetime();
			fields[13] = theForm.getUserID();
			
			sql = servSql.getUpdateSql();
			
			boolean  res = bSqlServ.dbWrite(cn, sql, fields);
			
			if(!res){
				common.setAppFailMsg(msg , " 更新记录失败，请检查！");
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return;
			}
			DbcpPool.commitTran(cn);
			DbcpPool.closeTran(cn);			
			DbcpPool.returnConnection(cn);
			
		} catch(Exception ex) 
		{
			logger.error("System error , param get about invite" + ex);
			common.setAppFailMsg(msg , ex.getMessage());
		} finally {
			DbcpPool.returnConnection(cn);
		}
				
		return ;
	}

}
